﻿var speakerIDs = [];
$(function () {
    var data;
    $('#descrEditor').ckeditor();
    $('#txtSpeakerNameSearch').watermark('Type the first name...');
    var id = querySt('id');
    ajaxCall('../DataService.asmx/GetAllEvents', false, "",
                function (msg) {
                    for (i in msg.d) {
                        $('#ddlEvents').append($('<option></option>').val(msg.d[i].EventID).html(msg.d[i].EventName));
                    }
                },
                function () {
                    alert("Error happend while reading the events from database");
                }
            );

    ajaxCall('../DataService.asmx/GetAllSpeakers', false, "",
                function (msg) {
                    debugger;
                    for (i in msg.d) {
                        $('#speakers').append($('<input type="checkbox" class="speakerCb"/>').val(msg.d[i].ID));
                        $('#speakers').append($('<label id="' + msg.d[i].ID + '"></label>').html(msg.d[i].FirstName + " " + msg.d[i].LastName + "<br/>"));
                     
                    }
                },
                function () {
                    alert("Error happend while reading the speakers from database");
                }
            );
    
    
    if (id != "") {
        $('#btnInsert').html("Update session");
        $('#EditSession').css('visibility', 'visible');
        data = '{"id":"' + id + '"}';
        ajaxCall('../DataService.asmx/GetSessionById', false, data,
                    function (msg) {
                        $('#txtTitle').val(msg.d.Title);
                        $('#ddlLanguage').val(msg.d.Language);
                        $('#descrEditor').val(msg.d.Description);
                        $('#ddlEvents').val(msg.d.EventID);
                        for (i in msg.d.Speakers) {
                            $('.speakerCb').each(
                                function () {
                                    if ($(this).val() == msg.d.Speakers[i].ID)
                                        this.checked = true;
                                });
                        }
                    },
                    function () {
                        alert("Error happend while retrieving data from database");
                    }
                );
    }
    else {
        $('#EditSession').css('visibility', 'hidden');
    }


    $('#btnInsert').click(function () {
        var speakerList = [''];
        var i = 0;
        $('input[type=checkbox]').each(function () {
            if (this.checked) {
                speakerList[i] = $(this).val();
                i++;
            }
        });

        $('#validateSession').validate({
            rules: {
                title: "required",
                sessionDescription: "required"

            },
            messages: {
                title: "Title is required",
                sessionDescription: "Description is required"

            }
        });

        CKEDITOR.instances["descrEditor"].updateElement();

        if ($('#validateSession').valid()) {
            if (id == "") {
                data = "{'title': '" + $('#txtTitle').val() + "', 'description': '" + CKEDITOR.instances["descrEditor"].getData() + "', 'eventId': '" + $('#ddlEvents option:selected').val() + "',  'language': '" + $('#ddlLanguage option:selected').text() + "', 'speakers': '" + speakerList + "'}";
                ajaxCall('../DataService.asmx/InsertSession', false, data,
                            function (msg) {
                                $('#txtTitle').val("");
                                $('#descrEditor').val("");
                                $('input[type=checkbox]').each(function () {
                                    if (this.checked) {
                                        $(this).attr("checked", false);
                                    }
                                });
                            },
                            function () {
                                alert("Error happend while inserting data");
                            }
                        );
            }
            else {
                data = "{'id':'" + id + "', 'title': '" + $('#txtTitle').val() + "', 'description': '" + CKEDITOR.instances["descrEditor"].getData() + "', 'eventId': '" + $('#ddlEvents option:selected').val() + "',  'language': '" + $('#ddlLanguage option:selected').text() + "', 'speakers': '" + speakerList + "'}";
                ajaxCall('../DataService.asmx/UpdateSession', false, data,
                            function () {
                                alert("Update successfull");
                                window.location = "ManageSessions.aspx";
                            },
                            function () {
                                alert("Error happend while updating data");
                            }
                );
            }
        }

    });

});

function checkIfSpeakerSelected() {
    var checked = $('input[type=checkbox]:checked').length;
    if (checked == 0) {
        $('#lblSpeakerError').css('display', 'inherit');
        return false;
    }
    else {
        $('#lblSpeakerError').css('display', 'none');
        return true;
    }
}

function GetSpeakersByFirstName() {
    var flag = 0;
    ajaxCall('../DataService.asmx/GetSpeakersByName', false, '{"speakerName":"' + $('#txtSpeakerNameSearch').val() + '"}',
            function (msg) {
                if (msg.d == null) {
                    $('input[type=checkbox]').each(function () {
                        debugger;
                        $(this).css('display', 'none');
                        $('#' + $(this).val()).css('display', 'none');
                    });

                } else {
                    $('input[type=checkbox]').each(function () {
                        for (i in msg.d) {
                            if ($(this).val() == msg.d[i].ID) {
                                flag = 1;
                                $(this).css('display', 'initial');
                                $('#' + $(this).val()).css('display', 'initial');
                            } 
                        }
                        if (flag == 0) {
                            $(this).css('display', 'none');
                            $('#' + $(this).val()).css('display', 'none');

                        }
                        flag = 0;
                    });
                  
                }
            },
            function () {

            }
        );
        }


